package com.heroh.kettle.util;

import com.heroh.kettle.entity.ParamDbConnection;
import org.pentaho.di.core.KettleEnvironment;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.job.JobHopMeta;
import org.pentaho.di.job.JobMeta;
import org.pentaho.di.job.entries.abort.JobEntryAbort;
import org.pentaho.di.job.entries.special.JobEntrySpecial;
import org.pentaho.di.job.entries.success.JobEntrySuccess;
import org.pentaho.di.job.entries.trans.JobEntryTrans;
import org.pentaho.di.job.entry.JobEntryCopy;
import org.pentaho.di.trans.TransMeta;

import java.util.List;
import java.util.Map;

/**
 * job 生成工具类
 */
public class KettleJobUtil {

    public static JobMeta initJobMeta(List<ParamDbConnection> conns){
        try {
            KettleEnvironment.init();
        } catch (KettleException e) {
            e.printStackTrace();
        }
        JobMeta jobMeta = new JobMeta();
        // 设置数据库连接
        KettleCommonUtil.setDatabases(jobMeta, conns);
        // 设置作业日志
        KettleJobLog.setJobLog(jobMeta);
        return jobMeta;
    }

    public static JobMeta initJobMeta(String fileName, List<ParamDbConnection> conns, Map<String, String> dbMap){
        try {
            KettleEnvironment.init();
        } catch (KettleException e) {
            e.printStackTrace();
        }
        JobMeta jobMeta = new JobMeta();
        jobMeta.setFilename(fileName);
        // 设置数据库连接
        KettleCommonUtil.updateDatabases(jobMeta, conns, dbMap);
        // 设置作业日志
        KettleJobLog.setJobLog(jobMeta);
        return jobMeta;
    }


    /**
     * 生成开始节点
     * @return
     */
    public static JobEntryCopy setStart(JobMeta jobMeta){
        JobEntrySpecial start = new JobEntrySpecial();
        start.setName("START");
        start.setStart(true);
        JobEntryCopy startEntry = new JobEntryCopy(start);
        // 放入spoon
        startEntry.setDrawn(true);
        jobMeta.addJobEntry(startEntry);
        return startEntry;
    }

    /**
     * 生成成功节点
     * @return
     */
    public static JobEntryCopy setSuccess(JobMeta jobMeta){
        JobEntrySuccess success = new JobEntrySuccess();
        success.setName("Success");
        JobEntryCopy successEntry = new JobEntryCopy(success);
        jobMeta.addJobEntry(successEntry);
        return successEntry;
    }

    /**
     * 设置转换节点
     * @return
     */
    public static JobEntryCopy setTrans(JobMeta jobMeta, TransMeta transMeta){
        JobEntryTrans trans = new JobEntryTrans();
        trans.setMetaStore(transMeta.getMetaStore());
        trans.setTransname(transMeta.getName());
        JobEntryCopy transEntry = new JobEntryCopy(trans);
        jobMeta.addJobEntry(transEntry);
        return transEntry;
    }

    /**
     * 生成中止节点
     * @return
     */
    public static JobEntryCopy setAbort(JobMeta jobMeta){
        JobEntryAbort abort = new JobEntryAbort();
        abort.setName("Abort Job");
        JobEntryCopy abortEntry = new JobEntryCopy(abort);
        jobMeta.addJobEntry(abortEntry);
        return abortEntry;
    }

    public static void setJobHop(JobMeta jobMeta, JobEntryCopy from, JobEntryCopy to){
        JobHopMeta hop = new JobHopMeta(from, to);
        hop.setEvaluation(true);
        jobMeta.addJobHop(hop);
    }

    public static JobMeta generateJob(JobMeta jobMeta) {
        // 开始
        JobEntryCopy startEntry = setStart(jobMeta);
        JobEntryCopy successEntry = setSuccess(jobMeta);
        setJobHop(jobMeta, startEntry, successEntry);
        return jobMeta;
    }
}
